Page({
  data: {
    answers: [],
    user:null
  },
  onLoad() {
    this.loadData();
  },
  loadData() {
    // 调用接口获取数据
    this.fetchData();
  },
  fetchData() {
    // 获取全局对象
    const app = getApp();
    // 使用 wx.request 发送请求到后端接口
    wx.request({
      url: 'http://localhost:8080/hql/review/user', // 替换为你的服务器地址
      method: 'GET',
      data: {
        userId: app.globalData.user.id // 从全局变量中获取 userId
      },
      header: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + app.globalData.user.token // 在请求头中
      },
      success: (res) => {
        console.log('hui', res);
        if (res.data.code === 200) {
          const formattedData = res.data.data.map(item => {
            return {
              ...item,
              reviewTime: this.formatDate(item.reviewTime)
            };
          });
          this.setData({
            answers: formattedData,
            user: app.globalData.user
          });
          app.globalData.consultationReviews = formattedData;
        } else {
          console.error('请求失败:', res);
          wx.showToast({
            title: '获取数据失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        // 请求失败，处理错误
        console.error('请求失败:', err);
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        });
      }
    });
  },
  formatDate(dateString) {
    const date = new Date(dateString);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  }
});